package lyh.hnu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import lyh.hnu.common.BaseContext;
import lyh.hnu.common.R;
import lyh.hnu.entity.po.ShoppingCart;
import lyh.hnu.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //设置用户id，指定当前是哪个用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询当前菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        ShoppingCart sc;
        if (dishId != null) {
            //添加到购物车的是菜品
            sc = shoppingCartService.lambdaQuery()
                    .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                    .eq(ShoppingCart::getDishId, dishId)
                    .one();
        } else {
            //添加到购物车的是套餐
            sc = shoppingCartService.lambdaQuery()
                    .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                    .eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                    .one();
        }
        //如果已经存在，则在原来的数量上加一
        if (sc != null) {
            sc.setNumber(sc.getNumber() + 1);
            shoppingCartService.updateById(sc);
        } else {
            //如果不存在，则添加到购物车中
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            sc = shoppingCart;
        }
        return R.success(sc);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        List<ShoppingCart> list = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .orderByAsc(ShoppingCart::getCreateTime)
                .list();
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart dishOrSetmeal;
        if (shoppingCart.getDishId() != null) {
            dishOrSetmeal = shoppingCartService.lambdaQuery()
                    .eq(ShoppingCart::getDishId, shoppingCart.getDishId()).one();
        } else {
            dishOrSetmeal = shoppingCartService.lambdaQuery()
                    .eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId()).one();
        }
        if (dishOrSetmeal.getNumber() == 1) {
            shoppingCartService.removeById(dishOrSetmeal);
        } else {
            dishOrSetmeal.setNumber(dishOrSetmeal.getNumber() - 1);
            shoppingCartService.updateById(dishOrSetmeal);
        }
        return R.success("减少菜品(套餐)分数成功");
    }
}
